<?php

/**
 * @author Samrat Khan & Sadik Sarfaraz - 11:06:03 AM Aug 22, 2013 
 */
//require 'foo.config.php';
ob_start();
$host = 'localhost';
$user = 'root';
$pass = 'samraj77';
$db = 'final_warehouse';
//$table = 'stockin';
$file = 'Stockout';

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$values = mysql_query("
       SELECT t1.serialno,
            (SELECT projectname FROM projects WHERE t1.project_id = projects.project_id) AS ProjectName,
            t1.productid,
            t1.description,
            t1.revisionstate,
            (SELECT engname FROM engineerinfo WHERE t1.eng_id = engineerinfo.eng_id) AS EngineerName,
            (SELECT uniquename FROM responsetype WHERE t1.response_type_id = responsetype.responsetype_id) AS ResponseType,
            t1.challannumber,
            t1.out_quantity,
            (SELECT measurement FROM uom WHERE t1.uom = uom.uom_id) AS UOM,
            t1.requestnumber,
            t1.requestedby,
            t1.siteid,
            t1.boxno,
            (SELECT warehousename FROM warehouses WHERE t1.warehouseid = warehouses.warehouse_id) AS WarehouseName,
            t1.challannumber,
            t1.requesttime,
            t1.receivedtime,
            t1.deliverytime,
            t1.sla,
            TIMESTAMPDIFF(SECOND,`requesttime`,`deliverytime`) < TIME_TO_SEC(`sla`) as `CheckSla`,
            t1.receivername,
            t1.cellnumber,
            t1.transportername,
            (SELECT employeename FROM employee WHERE t1.employee_id = employee.employee_id) AS EmployeeName,
            t1.remarks, 
            (SELECT username FROM wms_users WHERE t1.userid = wms_users.id) AS UserName,
            (SELECT cusname FROM customerinfo WHERE t1.cus_id = customerinfo.cus_id) AS CustomerName,
            t1.inserteddate
        FROM stockout AS t1 WHERE junkentry = '1'
        ");
$headings = array( 'SerialNo', 'Project Name','ProductID','Description','Revision State', 
                    'Engineer Name','Response Type', 'Docket Number','Stock Out Quantity', 
                    'UOM', 'Request Number','Requested By','Site Name','Box Number','Warehouse Name',
                    'Challan Number','Request Time','Received Time','Delivery Time','SLA','Check SLA','Receiver Name',
                    'Mobile Number','Transporter Name','Employee Name','Remarks','Username','Customer Name','Inserted Date');
$i = 0;
foreach ($headings as $heading) {
    $csv_output .= $heading . ", ";
    $i++;
}
$csv_output .= "\n";
while ($rowr = mysql_fetch_row($values)) {
for ($j = 0; $j < $i; $j++) {
        $csv_output .= '"'.$rowr[$j] . '",';
    }
$csv_output .= "\n";
}
ob_end_clean();



$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
//header("Content-type: text/csv");
header('Content-Length: ' . strlen($csv_output) );
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>